﻿using HuaweiCloud.Dotnet.Samples.DMS4RabbitMQ.Message;
using MassTransit;

namespace HuaweiCloud.Dotnet.Samples.DMS4RabbitMQ.Worker
{
    public class HelloWorker : BackgroundService
    {
        private readonly IBus _publishEndpoint;
        public HelloWorker(IBus publishEndpoint)
        {
            _publishEndpoint = publishEndpoint;
        }
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            // 模拟生产者定时发送消息到DMS
            // 当任务没有被取消
            while (!stoppingToken.IsCancellationRequested)
            {
                // 生产消息
                await _publishEndpoint.Publish<IHelloDMS>(new
                {
                    Name = "HuaweiCloud DMS",
                    Time = DateTime.Now
                }, stoppingToken);
                // 延迟 1000 毫秒
                await Task.Delay(1000, stoppingToken);
            }
        }
    }
}
